package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.MessageEvent;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.view.dialog.ComTwoBtnDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 副卡管理
 */
public class ManageCardFragment extends BaseFragment {
    public static ManageCardFragment newInstance(String cardId, String payLimit, String useLimit,String phone, String realName, String cardType) {
        ManageCardFragment manageCardFragment = new ManageCardFragment();
        Bundle bundle = new Bundle();
        bundle.putString("cardId", cardId);
        bundle.putString("payLimit", payLimit);
        bundle.putString("useLimit", useLimit);
        bundle.putString("phone", phone);
        bundle.putString("realName", realName);
        bundle.putString("cardType", cardType);
        manageCardFragment.setArguments(bundle);
        return manageCardFragment;
    }

    @BindView(R.id.ll_jiebang)
    ConstraintLayout llJiebang;
    @BindView(R.id.ll_bu_card)
    ConstraintLayout llBuCard;
    @BindView(R.id.ll_mamager_money)
    ConstraintLayout llManagerMoney;
    private String cardId;
    private String payLimit;
    private String useLimit;
    private String phone;
    private String realName;
    private String cardType;
    private ComTwoBtnDialog comTwoBtnDialog, guashiBtnDialog;

    @Override
    protected int initLayout() {
        return R.layout.fg_managercard;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.SubCardManager), getActivity());
    }

    @Override
    protected void initData(Context mContext) {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        cardId = getArguments().getString("cardId");
        payLimit = getArguments().getString("payLimit");
        useLimit = getArguments().getString("useLimit");
        phone = getArguments().getString("phone");
        realName = getArguments().getString("realName");
        cardType = getArguments().getString("cardType");
    }

    @Override
    protected void loadMethod() {

    }

    @OnClick({R.id.ll_jiebang, R.id.ll_guashi, R.id.ll_bu_card, R.id.ll_mamager_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_jiebang:
                showJiebang();
                break;
            case R.id.ll_guashi:
                showGuaShi();
                break;
            case R.id.ll_bu_card:
                start(BuCardFragment.newInstance(cardId, payLimit, phone, realName));
                break;
            case R.id.ll_mamager_money:
                start(EduMoneyFragment.newInstance(cardId, payLimit,useLimit, phone, realName, cardType));
                break;
        }
    }

    /**
     * 显示挂失对话窗
     */
    private void showGuaShi() {
        guashiBtnDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.GUA_SHI_CARD, null);
        guashiBtnDialog.show();
        guashiBtnDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                guashiBtnDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                lossCard(cardId);
            }
        });
    }

    /**
     * 显示解绑的对话窗
     */
    private void showJiebang() {
        comTwoBtnDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.JIE_BANG_CARD, null);
        comTwoBtnDialog.show();
        comTwoBtnDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                comTwoBtnDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                untyingCard(cardId);
            }
        });
    }

    /**
     * 解绑副卡操作
     */
    private void untyingCard(String cardId) {
        RequestUtils.UntyingCard(getContext(), cardId, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {
                ToastUtils.showToast(getContext(), "操作成功");
                EventBus.getDefault().post(new MessageEvent(MessageEvent.CARD));
                goBack(getActivity());
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
                goBack(getActivity());
            }
        });

    }


    /**
     * 挂失副卡操作
     */
    private void lossCard(String cardId) {
        RequestUtils.LossCard(getContext(), cardId, new MyObserver<BaseBean>(getContext()) {
            @Override
            public void onSuccess(BaseBean result) {

            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
                pop();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        switch (event.message) {
            case MessageEvent.UPDATEEDU:
                payLimit=event.content;
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
